package com.kit.data.kitopeso.device.work;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.work.ListenableWorker;
import androidx.work.WorkerParameters;
import androidx.work.impl.utils.futures.SettableFuture;

import com.google.common.util.concurrent.ListenableFuture;
import com.kit.data.kitopeso.PathMaping;
import com.kit.data.kitopeso.device.DeviceUtil;
import com.kit.data.kitopeso.net.RxHttp;
import com.kit.data.kitopeso.net.U;
import com.kit.data.kitopeso.tools.AESCryptUtilJar;
import com.kit.data.kitopeso.tools.JsonParse;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.SpUtils;

import java.util.HashMap;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class KitoMsgInfoWork extends ListenableWorker {

    @SuppressLint("RestrictedApi")
    private SettableFuture<Result> mFuture;

    /**
     * @param appContext   The application {@link Context}
     * @param workerParams Parameters to setup the internal state of this worker
     */
    public KitoMsgInfoWork(@NonNull Context appContext, @NonNull WorkerParameters workerParams) {
        super(appContext, workerParams);
    }

    @SuppressLint("RestrictedApi")
    @NonNull
    @Override
    public ListenableFuture<Result> startWork() {

        mFuture = SettableFuture.create();
        String token = SpUtils.get_string(getApplicationContext(), SpUtils.KEY_TOKEN, "");
        DeviceUtil dt = new DeviceUtil(getApplicationContext(), U.URL_MAIN_PRODUCT, token);
        String json = dt.fetchMsg(PathMaping.PathList[36]);
        HashMap<String, String> heads = new HashMap<>();
        heads.put("product", U.URL_MAIN_PRODUCT);
        if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
            heads.put("country", U.URL_COUNTRY);
        }
        String data = AESCryptUtilJar.encryptDefault(json);
        LogUtil.e("me", json + "<-KitoMsgInfoWork post->" + data);
        RxHttp.post(U.commonUrl, data, heads)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                        if (!TextUtils.isEmpty(s)) {
                            String json1 = AESCryptUtilJar.decryptDefault(s);
                            LogUtil.e("me", "KitoMsgInfoWork json->" + json1);
                            if (JsonParse.getCode(json1)) {
                                mFuture.set(Result.success());
                            } else {
                                mFuture.set(Result.failure());
                            }
                        } else {
                            mFuture.set(Result.failure());
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        mFuture.set(Result.failure());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        return mFuture;
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void onStopped() {
        super.onStopped();
        if (mFuture != null && !mFuture.isDone()) {
            mFuture.cancel(true);
        }
    }

}
